#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
@author:atuo.cn
'''

from tkinter import *
from tkinter.ttk import *
from itertools import chain
import re

from dialogs.hintsdialog_layout import HintsDialogLayout
from assembler.parser import ALL_HINTS

class HintsDialog(HintsDialogLayout):
    
    DEFAULT_HINTS=["MaxSectionTitleLen","IgnoreEmptySection","FilterKeywords"]
    HINT_OPTS={
        k:ALL_HINTS[k] for k in DEFAULT_HINTS
    }
    
    def __init__(self, master, wmtitle,hints=None):
        self.hints=hints or HintsDialog.HINT_OPTS
        HintsDialogLayout.__init__(self, master, wmtitle)

    def onLoad(self):
        for k,v in self.hints.items():
            #skip MaxSectionTitleLen
            if k == "MaxSectionTitleLen": continue
            
            self.opts[k].set(int(bool(v)))
            if isinstance(v, list):
                self.opts["%s_String"%k].set(",".join(v))
        keys = self.opts.keys()-self.hints.keys()
        for k in keys:
            if not k.endswith("_String"):
                self.opts[k].set(0)
        
    #override methods of Dialog
    def apply(self):
        hints={}
        hints["IgnoreEmptySection"]=self.opts["IgnoreEmptySection"].get()
        if self.opts["FilterKeywords"].get():
            li=re.split(
                ",;，；",
                self.opts["FilterKeywords_String"].get()
            )
            hints["FilterKeywords"]=list(filter(lambda s:s, li))
             
        if self.opts["Prefix"].get():
            li=re.split(
                ",;，；",            
                self.opts["Prefix_String"].get()
            )
            hints["Prefix"]=list(filter(lambda s:s, li))
             
        self.hints=hints

    #Event Handlers
        